package ee.ut.math.chess.hadoop;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import ee.ut.math.chess.base.model.Move;
import ee.ut.math.chess.game.model.Game;

public class HadoopManager {
	protected static ExecutorService threadPool = Executors.newCachedThreadPool();
	public static void doComputerMove(final Game game) {
		threadPool.execute(new Runnable() {
			
			public void run() {
				try {
					Skeleton skeleton = new Skeleton();
					System.out.println(game.toJSON());
					
					Move move = skeleton.getMove(game);
					game.move(move);
					
					System.out.println(game.toJSON());
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public static byte[] getBytesFromGame(Game game) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ObjectOutputStream os = new ObjectOutputStream(out);
        os.writeObject(game);
        return out.toByteArray();
    }

    public static Game getGameFromBytes(byte[] data) throws IOException, ClassNotFoundException {
        ByteArrayInputStream in = new ByteArrayInputStream(data);
        ObjectInputStream is = new ObjectInputStream(in);
        return (Game) is.readObject();
    }
}
